<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div><el-button type="warning" @click="addCategory" icon="el-icon-plus">添加分类</el-button></div>
      <div class="ml-2"><el-button type="danger" icon="el-icon-delete">批量删除</el-button></div>
    </div>
    <div class="flex-1 ml-2">
      <el-table :data="categoriesList" border stripe>
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="序号" type="index" width="65" />
        <el-table-column align="center" label="ID" prop="id" width="65" />
        <el-table-column align="center" label="分类名称" prop="name" width="130" />
        <el-table-column align="center" label="排序" prop="sort" width="45" />
        <el-table-column align="center" label="添加时间" prop="create_time" width="130" />
        <el-table-column align="center" label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
            <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex-1 mt-3">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="total">
        </el-pagination>
      </div>
      <el-dialog :visible.sync="dialogVisible" width="30%" title="添加分类">
        <el-form :model="form" ref="form" label-width="80px">
          <el-form-item label="名称" prop="name">
            <div style="width: 50%;">
              <el-input v-model="form.name"></el-input>
            </div>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <div style="width: 18%;">
              <el-input v-model="form.sort"></el-input>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </d2-container>
</template>

<script>
  import {
    classificationListUrl,
    classificationCreateUrl,
    classificationDetailUrl,
    classificationUpdateUrl,
    classificationDeleteUrl,
  } from '@/utils/picture'
  export default {
    name: 'productCategory',
    data() {
      return {
        categoriesList: [],
        total: 0,
        dialogVisible: false,
        form: {
          name: '',
          sort: ''
        },
        waitState: true,
        queryInfo: {
          page: 1,
          page_size: 10
        },
        getCategoryId:''
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getClassifications()
      },
      getClassifications() {
        this.$get(classificationListUrl, this.queryInfo).then(res => {
          this.categoriesList = res.data.data.list
          this.total = res.data.data.total
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.page_size = newSize
        this.getCategoriesList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getCategoriesList()
      },
      addCategory() {
        this.dialogVisible = true
      },
      // 提交表单
      submitForm() {
        if (this.waitState) {
          this.$post(classificationCreateUrl, this.form).then(res => {
            if (res.data.code == 200) {
              this.getClassifications()
              this.dialogVisible = false
              this.$message.success('添加成功')
              this.form = {}
            }
          })
        }else{
          this.$put(classificationUpdateUrl + this.getCategoryId + '/', this.form).then(res => {
            if (res.data.code == 200) {
              this.getClassifications()
              this.dialogVisible = false
              this.waitState = true
              this.$message.success('修改成功')
              this.form = {}
            }
          })
        }

      },
      openDelete(catagoryId) {
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.handleDelete(catagoryId);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleDelete(categoryId) {
        this.$del(classificationDeleteUrl + categoryId + '/').then(res => {
          this.getClassifications()
        })
      },
      handleEdit(categoryId) {
        this.waitState = false
        this.dialogVisible = true
        this.getCategoryId = categoryId
        this.$get(classificationDetailUrl + categoryId + '/').then(res => {
          if (res.data.code == 200) {
            this.form = {
              name: res.data.data.name,
              sort: res.data.data.sort
            }
          }
        })
      }
    }
  }
</script>
<style lang="scss">

</style>
